﻿using CardMaker.Library.Models;
using Microsoft.JSInterop;

namespace CardMaker.Library.Pages;

public partial class CreateCard {
    private CardTemplate _card = new();
    private readonly CardTemplate _cardTemplate = new();
    private bool _isLoading=true;
    private byte[] frameImage;
    private byte[] sideiamgeBytes;
    private byte[] mainImgBytes;
    private List<SideImage> trSideImage;
    private string frameImg;
    private string sideImageImg;
    private string sideImg1;
    private string sideImg2;
    private string mainImg;
    List<string> imagList = new List<string>();
    protected override void OnAfterRender(bool firstRender) {
        if (!firstRender)
            return;
        Task.Run(async () => {
            await _sideImageStorage.InitializeAsync();
            await _cardImageToUrlService.InitModuleAsync();
            //todo
            await _cardTemplateStorage.InitializeAsync();
            await _cardImageToUrlService.InitModuleAsync();
            await _frameImageStorage.InitializeAsync();

            /*await _mainImageStorage.InitializeAsync();*/
            _isLoading = true;
            _cardTemplate.CardTemplateId = 2;
            _cardTemplate.FrameName = "图片尝试2";
            _cardTemplate.TemplateName = "第二模板尝试";
            var framepath = Path.GetFullPath(@"Resources\\FrameImageTemp.png");
            var frameImageMemoryStream = new MemoryStream();
            await using var frameImageFileStream =
                new FileStream(framepath, FileMode.Open);
            await frameImageFileStream.CopyToAsync(
                frameImageMemoryStream);
            var sidepath = Path.GetFullPath(@"Resources\\3女皇.png");
            var sideImageMemoryStream = new MemoryStream();
            await using var sideimageFileStream =
                new FileStream(sidepath, FileMode.Open);
            await sideimageFileStream.CopyToAsync(
                sideImageMemoryStream);
            var sideImages =
                new List<SideImage> {
                    new(1, 0, "A组副图", "452254",3, "100"),
                    new(1, 1, "B组副图", "18681861",2, "50"),
                    /*new(1, 2, "C组副图", "18681861",1, "50")*/
                };
            await sideImages[0].AddKeXuan("3女皇", sideImageMemoryStream.ToArray());
            
            /*var mainpath = Path.GetFullPath(@"Resources\\主图.png");
            var mainImageMemoryStream = new MemoryStream();
            await using var mainImageFileStream = new FileStream(mainpath, FileMode.Open);
            await mainImageFileStream.CopyToAsync(mainImageMemoryStream);
            await _mainImageStorage.SaveMainImageAsync(_cardTemplate, "主图", mainImageMemoryStream.ToArray());
            mainImgBytes = await _mainImageStorage.GetMainImagesAsync(_cardTemplate, "主图");
            mainImg = await _cardImageToUrlService.GenerateImageUrl(mainImgBytes);*/

            //A组副图
            var sidepath1 = Path.GetFullPath(@"Resources\\4皇帝.png");
            var sideImageMStream1 = new MemoryStream();
            await using var sideFile1 = new FileStream(sidepath1, FileMode.Open);
            await sideFile1.CopyToAsync(sideImageMStream1);

            var sidepath2 = Path.GetFullPath(@"Resources\\5教皇.png");
            var sideImageMStream2 = new MemoryStream();
            await using var sideFile2 = new FileStream(sidepath2, FileMode.Open);
            await sideFile2.CopyToAsync(sideImageMStream2);
            
            await sideImages[0].AddKeXuan("4皇帝", sideImageMStream1.ToArray());
            await sideImages[0].AddKeXuan("5教皇", sideImageMStream2.ToArray());
            /*_card = await _cardTemplateStorage.CreateCardTemplate(card, sideImage1, textFrames);*/
            /*_card = await _cardTemplateService.GetCardTemplateAsync(2);*/
            /*sideiamgeBytes = (await _sideImageStorage.GetSideImagesAsync(_card))[0].SideKeXuanImageBytes[0];*/
            /*sideImg1 = await _cardImageToUrlService.GenerateImageUrl(sideiamgeBytes);*/

            //B组副图
            var sidepath3 = Path.GetFullPath(@"Resources\\6恋人.png");
            var sideImageMStream3 = new MemoryStream();
            await using var sideFile3 = new FileStream(sidepath3, FileMode.Open);
            await sideFile3.CopyToAsync(sideImageMStream3);
            var sidepath4 = Path.GetFullPath(@"Resources\\7战车.png");
            var sideImageMStream4 = new MemoryStream();
            await using var sideFile4 = new FileStream(sidepath4, FileMode.Open);
            await sideFile4.CopyToAsync(sideImageMStream4);
            
            await sideImages[1].AddKeXuan("6恋人", sideImageMStream3.ToArray());
            await sideImages[1].AddKeXuan("7战车", sideImageMStream4.ToArray());

            for (int j = 0; j < sideImages.Count; j++) {
                for (int i = 0; i < sideImages[j].SideKeXuanImageBytes.Count; i++)
                {
                    imagList.Add(
                        await _cardImageToUrlService.GenerateImageUrl(sideImages[j].SideKeXuanImageBytes[i]));
                }
            }

            

            /*
            sideImageImg =
                await _cardImageToUrlService.GenerateImageUrl(sideImages[0]
                    .SideKeXuanImageBytes[1]);
            sideImg1 =
                await _cardImageToUrlService.GenerateImageUrl(sideImages[0]
                    .SideKeXuanImageBytes[2]);*/

            var textFrames = new List<TextFrame> {
                new() {
                    TextFrameId = 1,
                    TextFrameName = "Frame1",
                    TextFramePosition = "TopLeft",
                    Text = "Sample Text 1"
                }
            };
            _card = await _cardTemplateStorage.CreateCardTemplate(_cardTemplate,
                sideImages, textFrames);//此处在测试存取，其中CardTemplateId=1 是可以不设置的，此处为了对图读写方便，也为了避免每次运行生成过多冗余数据，固定了Id
            _card = await _cardTemplateService.GetCardTemplateAsync(2);
            await _sideImageStorage.SaveSideImageAsync(sideImages);
            sideImageMemoryStream.Close();
            sideImageMStream1.Close();
            sideImageMStream2.Close();
            sideImageMStream3.Close();
            sideImageMStream4.Close();


            /*sideiamgeBytes =
    (await _sideImageStorage.GetSideImagesAsync(_card))
    [0].SideKeXuanImageBytes[0];//使用单一副图代替。需要执行请在SideImageBox文件下放置图片 1.1.测试可选项1.png
sideImageImg =await _cardImageToUrlService.GenerateImageUrl(sideiamgeBytes);*/

            await _frameImageStorage.SaveFrameImageAsync(_card, frameImageMemoryStream.ToArray());
            frameImage=await _frameImageStorage.GetFrameImagesAsync(_card);

            frameImg = await _cardImageToUrlService.GenerateImageUrl(frameImage);//1.图片尝试.png 在C:\Users\Cold Maker\AppData\Local\FrameImageBox 此路径下
            //对原生方法进行了改进，下方被注释掉的代码作废
            //todo 疑似没有正确引入脚本
            /*await _jsRuntime.InvokeVoidAsync("setImage",
                new DotNetStreamReference(
                    new MemoryStream(frameImage)), "image");*/

            _isLoading = false;
            await InvokeAsync(StateHasChanged);

            /*await _cardImageToUrlService.RevokeUrl();//释放图片资源*/
        });
         /*async Task<MemoryStream> LoadImage(string path)
        {
            var fullPath = Path.GetFullPath(path);
            await using var stream = new FileStream(fullPath, FileMode.Open);
            var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            return memoryStream;
        }*/
    }
}